{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Client\n",
    "\n",
    "Demo of a client interacting with a remote conversational retrieval chain. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can interact with this via API directly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'output': {'content': 'Based on the given context, the information we have about Harrison is that he worked at Kensho.',\n",
       "  'additional_kwargs': {},\n",
       "  'type': 'ai',\n",
       "  'example': False},\n",
       " 'callback_events': [],\n",
       " 'metadata': {'run_id': '3455df2b-93f8-4e67-b1a3-27f90670cf7b'}}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "inputs = {\"input\": {\"question\": \"what do you know about harrison\", \"chat_history\": []}}\n",
    "response = requests.post(\"http://localhost:8000/invoke\", json=inputs)\n",
    "\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also interact with this via the RemoteRunnable interface (to use in other chains)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langserve import RemoteRunnable\n",
    "\n",
    "remote_runnable = RemoteRunnable(\"http://localhost:8000/\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Remote runnable has the same interface as local runnables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='Based on the given context, the only information we have about Harrison is that he worked at Kensho.')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "await remote_runnable.ainvoke({\"question\": \"what do you know about harrison\", \"chat_history\": []})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "AIMessage(content='Harrison worked at Kensho.')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "await remote_runnable.ainvoke(\n",
    "    {\"question\": \"what do you know about harrison\", \"chat_history\": [(\"hi\", \"hi\")]}\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content=''\n",
      "content='H'\n",
      "content='arrison'\n",
      "content=' worked'\n",
      "content=' at'\n",
      "content=' Kens'\n",
      "content='ho'\n",
      "content='.'\n",
      "content=''\n"
     ]
    }
   ],
   "source": [
    "async for chunk in remote_runnable.astream(\n",
    "    {\"question\": \"what do you know about harrison\", \"chat_history\": [(\"hi\", \"hi\")]}\n",
    "):\n",
    "    print(chunk)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "stream log shows all intermediate steps as well!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RunLogPatch({'op': 'replace',\n",
      "  'path': '',\n",
      "  'value': {'final_output': None,\n",
      "            'id': '2ff5a98d-49f0-40ae-92fe-489c3047d1c3',\n",
      "            'logs': {},\n",
      "            'streamed_output': []}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'ffdda3c9-a0ba-49a6-af18-c01748c31801',\n",
      "            'metadata': {},\n",
      "            'name': 'RunnableParallel',\n",
      "            'start_time': '2023-11-16T15:59:23.348',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:1'],\n",
      "            'type': 'chain'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableSequence',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'a9da3f2c-f3ae-44c1-a2f4-1035faa0d1c2',\n",
      "            'metadata': {},\n",
      "            'name': 'RunnableSequence',\n",
      "            'start_time': '2023-11-16T15:59:23.349',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['map:key:standalone_question'],\n",
      "            'type': 'chain'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '4d32b08f-a989-4c01-8068-479bed506d99',\n",
      "            'metadata': {},\n",
      "            'name': 'RunnableParallel',\n",
      "            'start_time': '2023-11-16T15:59:23.349',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:1'],\n",
      "            'type': 'chain'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'c7b9bd78-cbb4-41f9-b31a-50a5f8940a8d',\n",
      "            'metadata': {},\n",
      "            'name': '<lambda>',\n",
      "            'start_time': '2023-11-16T15:59:23.350',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['map:key:chat_history'],\n",
      "            'type': 'chain'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>/final_output',\n",
      "  'value': {'output': '\\nHuman: hi\\nAssistant: hi'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/<lambda>/end_time',\n",
      "  'value': '2023-11-16T15:59:23.350'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel:2/final_output',\n",
      "  'value': {'chat_history': '\\nHuman: hi\\nAssistant: hi'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableParallel:2/end_time',\n",
      "  'value': '2023-11-16T15:59:23.351'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/PromptTemplate',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'ef9f8729-2d4a-4887-86a8-a284d64f5882',\n",
      "            'metadata': {},\n",
      "            'name': 'PromptTemplate',\n",
      "            'start_time': '2023-11-16T15:59:23.351',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:2'],\n",
      "            'type': 'prompt'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/PromptTemplate/final_output',\n",
      "  'value': StringPromptValue(text='Given the following conversation and a follow up question, rephrase the \\nfollow up question to be a standalone question, in its original language.\\n\\nChat History:\\n\\nHuman: hi\\nAssistant: hi\\nFollow Up Input: what do you know about harrison\\nStandalone question:')},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/PromptTemplate/end_time',\n",
      "  'value': '2023-11-16T15:59:23.351'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '7cda5923-ed2a-42ea-aee7-a2391371ff2f',\n",
      "            'metadata': {},\n",
      "            'name': 'ChatOpenAI',\n",
      "            'start_time': '2023-11-16T15:59:23.352',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:3'],\n",
      "            'type': 'llm'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/StrOutputParser',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '6a0e199d-51fa-4306-8a7d-054444c6855f',\n",
      "            'metadata': {},\n",
      "            'name': 'StrOutputParser',\n",
      "            'start_time': '2023-11-16T15:59:24.613',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:4'],\n",
      "            'type': 'parser'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel:3',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '74a13de1-c2d1-43c0-813a-7ce0246118b3',\n",
      "            'metadata': {},\n",
      "            'name': 'RunnableParallel',\n",
      "            'start_time': '2023-11-16T15:59:24.616',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:2'],\n",
      "            'type': 'chain'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableSequence:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '625e2183-7ee5-4f1f-8f47-56541afc96ee',\n",
      "            'metadata': {},\n",
      "            'name': 'RunnableSequence',\n",
      "            'start_time': '2023-11-16T15:59:24.619',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['map:key:context'],\n",
      "            'type': 'chain'}})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output_str/-',\n",
      "  'value': 'What'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output_str/-',\n",
      "  'value': ' information'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output_str/-',\n",
      "  'value': ' do'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output_str/-',\n",
      "  'value': ' you'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output_str/-',\n",
      "  'value': ' have'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output_str/-',\n",
      "  'value': ' about'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output_str/-',\n",
      "  'value': ' Harrison'})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': '?'})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/final_output',\n",
      "  'value': LLMResult(generations=[[ChatGenerationChunk(text='What information do you have about Harrison?', generation_info={'finish_reason': 'stop'}, message=AIMessageChunk(content='What information do you have about Harrison?'))]], llm_output=None, run=None)},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/end_time',\n",
      "  'value': '2023-11-16T15:59:24.832'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/StrOutputParser/final_output',\n",
      "  'value': {'output': 'What information do you have about Harrison?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/StrOutputParser/end_time',\n",
      "  'value': '2023-11-16T15:59:24.833'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableSequence/final_output',\n",
      "  'value': {'output': 'What information do you have about Harrison?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableSequence/end_time',\n",
      "  'value': '2023-11-16T15:59:24.834'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel/final_output',\n",
      "  'value': {'standalone_question': 'What information do you have about '\n",
      "                                   'Harrison?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableParallel/end_time',\n",
      "  'value': '2023-11-16T15:59:24.835'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableLambda',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '71b2e8dc-753f-4bf2-83de-b87b26828370',\n",
      "            'metadata': {},\n",
      "            'name': 'RunnableLambda',\n",
      "            'start_time': '2023-11-16T15:59:24.837',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:1'],\n",
      "            'type': 'chain'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableLambda/final_output',\n",
      "  'value': {'output': 'What information do you have about Harrison?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableLambda/end_time',\n",
      "  'value': '2023-11-16T15:59:24.837'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/Retriever',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'd3d6254d-d073-478f-b00b-bc27eafa24fd',\n",
      "            'metadata': {},\n",
      "            'name': 'Retriever',\n",
      "            'start_time': '2023-11-16T15:59:24.839',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:2', 'FAISS', 'OpenAIEmbeddings'],\n",
      "            'type': 'retriever'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '77f7132a-d98f-4121-89cf-10e462c26496',\n",
      "            'metadata': {},\n",
      "            'name': '<lambda>',\n",
      "            'start_time': '2023-11-16T15:59:24.839',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['map:key:question'],\n",
      "            'type': 'chain'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/<lambda>:2/final_output',\n",
      "  'value': {'output': 'What information do you have about Harrison?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/<lambda>:2/end_time',\n",
      "  'value': '2023-11-16T15:59:24.840'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/Retriever/final_output',\n",
      "  'value': {'documents': [Document(page_content='harrison worked at kensho')]}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/Retriever/end_time',\n",
      "  'value': '2023-11-16T15:59:25.074'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/_combine_documents',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '82aecf3e-ca9d-4b48-a281-03f8e834ea62',\n",
      "            'metadata': {},\n",
      "            'name': '_combine_documents',\n",
      "            'start_time': '2023-11-16T15:59:25.075',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:3'],\n",
      "            'type': 'chain'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/_combine_documents/final_output',\n",
      "  'value': {'output': 'harrison worked at kensho'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/_combine_documents/end_time',\n",
      "  'value': '2023-11-16T15:59:25.075'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableSequence:2/final_output',\n",
      "  'value': {'output': 'harrison worked at kensho'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableSequence:2/end_time',\n",
      "  'value': '2023-11-16T15:59:25.075'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/RunnableParallel:3/final_output',\n",
      "  'value': {'context': 'harrison worked at kensho',\n",
      "            'question': 'What information do you have about Harrison?'}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/RunnableParallel:3/end_time',\n",
      "  'value': '2023-11-16T15:59:25.076'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatPromptTemplate',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'b37f36ce-3a27-4402-81c4-6893f03ec179',\n",
      "            'metadata': {},\n",
      "            'name': 'ChatPromptTemplate',\n",
      "            'start_time': '2023-11-16T15:59:25.076',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:3'],\n",
      "            'type': 'prompt'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatPromptTemplate/final_output',\n",
      "  'value': {'messages': [HumanMessage(content='Answer the question based only on the following context:\\nharrison worked at kensho\\n\\nQuestion: What information do you have about Harrison?\\n')]}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatPromptTemplate/end_time',\n",
      "  'value': '2023-11-16T15:59:25.077'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'b1179088-eb7c-49c5-af1e-bd09c48408bf',\n",
      "            'metadata': {},\n",
      "            'name': 'ChatOpenAI',\n",
      "            'start_time': '2023-11-16T15:59:25.078',\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:4'],\n",
      "            'type': 'llm'}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='')})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI:2/streamed_output_str/-', 'value': ''})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='Based')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': 'Based'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' on')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' on'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' the')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' the'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' given')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' given'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' context')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' context'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=',')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ','})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' the')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' the'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' only')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' only'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' information')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' information'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' we')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' we'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' have')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' have'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' about')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' about'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' Harrison')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' Harrison'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' is')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' is'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' that')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' that'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' he')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' he'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' worked')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' worked'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' at')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' at'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' Kens')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' Kens'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='ho')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': 'ho'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='.')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '.'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='')})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI:2/streamed_output_str/-', 'value': ''})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/final_output',\n",
      "  'value': LLMResult(generations=[[ChatGenerationChunk(text='Based on the given context, the only information we have about Harrison is that he worked at Kensho.', generation_info={'finish_reason': 'stop'}, message=AIMessageChunk(content='Based on the given context, the only information we have about Harrison is that he worked at Kensho.'))]], llm_output=None, run=None)},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/end_time',\n",
      "  'value': '2023-11-16T15:59:26.547'})\n",
      "RunLogPatch({'op': 'replace',\n",
      "  'path': '/final_output',\n",
      "  'value': {'output': AIMessageChunk(content='Based on the given context, the only information we have about Harrison is that he worked at Kensho.')}})\n"
     ]
    }
   ],
   "source": [
    "async for chunk in remote_runnable.astream_log(\n",
    "    {\"question\": \"what do you know about harrison\", \"chat_history\": [(\"hi\", \"hi\")]}\n",
    "):\n",
    "    print(chunk)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
